/**
 * &lt;p&gt;
 * copyright &amp;copy;  2015, all rights reserved.
 * @author Administrator
 * @version $Id$
 * @since 1.0
 * 
 */
package com.concurrents.volatiles;

/** 
 * ①.状态标记量 Volatile<p>
 * @author LingMin 
 * @date 2017年4月7日<br>
 * @version 1.0<br>
 */
public class TestVolatileStatus implements Runnable{

	private volatile boolean flag = false;//volatile

	@Override
	public void run() {
		//线程1
		while(!flag){
		    doSomething();
		}
	}
	
	public void doSomething(){
		try {
			Thread.sleep(1 * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("doSomething...");
	}
	
	//线程2 
	public void setFlag(){
		this.flag = true;
	}
	
	/***
	 * <p>
	 * @param args <p>
	 * void
	 */
	public static void main(String[] args) {
		TestVolatileStatus test = new TestVolatileStatus();
		Thread thread1 = new Thread(test);
		thread1.start();//线程1启动
		
//		try {
//			Thread.sleep(5 * 1000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		
		Runnable runnable = new Runnable() {
			public void run() {
				//线程2 执行
				test.setFlag();
			}
		};
		Thread thread2 = new Thread(runnable);
		thread2.start();
		
	}
	
}
